import { App } from 'vue'

export default {
    install(app: App) {
        // 自定义全局指令
        app.directive('defaultBind', {
            mounted(el, binding) {
                console.log('el', el)
                console.log('binding', binding.value)
            },
        })
        console.dir(app)
        // // 混入（vue2推荐，vue3组合式api暂不常用）
        // app.mixin({
        //   created() {
        //     console.log('created in plugin')
        //   }
        // })
    },
}

export const isMobile = () => {
    const userAgent = navigator.userAgent || navigator.vendor || window?.opera as any;
    // 根据userAgent判断设备类型  //false为PC端，true为移动端
    return /iPad|iPhone|iPod|Android|Opera Mini|BlackBerry|IEMobile|webOS|Windows Phone|Silk/i.test(userAgent);
}

// 获取当前时间段
export const whatTime = () => {
    const hours = new Date().getHours()
    let message = ''
    if (hours <= 9) {
        message = '早上'
    } else if (hours <= 12) {
        message = '上午'
    } else if (hours <= 18) {
        message = '下午'
    } else {
        message = '晚上'
    }
    return message
}

// 批量引用本地图片
export const getAssetsFile = (file: string) => {
    // let fileName = `../assets/img/${file}`
    let development = window.location.origin.includes('127') ||  window.location.origin.includes('localhost')
    let production = window.location.origin.includes('101.200.90.7') || window.location.origin.includes('paperfrogwei')
    let fileName = ''
    if (development) {
        fileName = `../../public/img/${file}`
    } else if (production) {
        fileName = `../../img/${file}`
    } else {
        fileName = `../../img/${file}`
    }
    console.log('fileName', fileName)
    return new URL(fileName, import.meta.url).href
}

export const sourceHeader = () => {
    let sourceUrl = window.location.origin
    let sourceUrHeader = ''
    if (sourceUrl.includes('https://')) {
        sourceUrHeader = 'https://www.paperfrogwei.com'
    } else if (sourceUrl.includes('127') || sourceUrl.includes('localhost') ||  sourceUrl.includes('101.200.90.7') || sourceUrl.includes('http://')) {
        sourceUrHeader = 'http://www.paperfrogwei.com'
    } else {
        sourceUrHeader = 'http://101.200.90.7'
    }
    console.log('sourceUrHeader', sourceUrHeader)
    return sourceUrHeader
}

// 当前年月日
export const getTime = () => {
    const currentDate = new Date();
    const year = currentDate.getFullYear();
    const month = currentDate.getMonth() + 1;
    const day = currentDate.getDate();
    return `${year}年${month}月${day}日`
}

// 当前星期
export const getWeek = () => {
    const days = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
    const date = new Date();
    const dayOfWeek = days[date.getDay()];
    return dayOfWeek
}

// 当前时分秒
export const times = (flag: any) => {
    let currentTime = new Date(); // "Mon Sep 30 2019 20:51:28 GMT+0800 (中国标准时间)"
    let hours = currentTime.getHours();
    let minutes = currentTime.getMinutes();
    let seconds = currentTime.getSeconds();
    if (flag == '24') {
        // 24小时制
        return hours + "点" + minutes + "分"
    } else if (flag == '12') {
        // 12小时制
        if(hours > 12) {
        hours -= 12;
        }
        // hours = hours < 10 ? "0" + hours : hours;
        minutes = minutes < 10 ? '0' + minutes : minutes;
        seconds = seconds < 10 ? '0' + seconds : seconds;
        return hours + "点" + minutes + "分"
    }
}

// 全国省份列表
export const provinceList = () => {
    return [
        {city: '北京', value: '北京'},
        {city: '天津', value: '天津'},
        {city: '上海', value: '上海'},
        {city: '重庆', value: '重庆'},
        {city: '河北', value: '河北'},
        {city: '山西', value: '山西'},
        {city: '黑龙江', value: '黑龙江'},
        {city: '吉林', value: '吉林'},
        {city: '辽宁', value: '辽宁'},
        {city: '江苏', value: '江苏'},
        {city: '浙江', value: '浙江'},
        {city: '安徽', value: '安徽'},
        {city: '福建', value: '福建'},
        {city: '江西', value: '江西'},
        {city: '山东', value: '山东'},
        {city: '河南', value: '河南'},
        {city: '湖北', value: '湖北'},
        {city: '湖南', value: '湖南'},
        {city: '广东', value: '广东'},
        {city: '海南', value: '海南'},
        {city: '四川', value: '四川'},
        {city: '贵州', value: '贵州'},
        {city: '云南', value: '云南'},
        {city: '新疆', value: '新疆'},
        {city: '宁夏', value: '宁夏'},
        {city: '西藏', value: '西藏'},
        {city: '广西', value: '广西'},
        {city: '内蒙古', value: '内蒙古'},
        {city: '陕西', value: '陕西'},
        {city: '甘肃', value: '甘肃'},
        {city: '青海', value: '青海'},
    ]
}

// 全国城市列表
export const cityList = () => {
return [
    {
        city: '北京',
        adcode: '110000'
    },
    {
        city: '上海',
        adcode: '310100'
    },
    {
        city: '杭州',
        adcode: '330100'
    },
    {
        city: '嘉兴',
        adcode: '330400'
    },
    {
        city: '徐州',
        adcode: '320300'
    },
    {
        city: '天津',
        adcode: '120100'
    },
    {
        city: '南京',
        adcode: '320100'
    },
    {
        city: '长沙',
        adcode: '430100'
    },
    {
        city: '黄冈',
        adcode: '421100'
    },
    {
        city: '青岛',
        adcode: '370200'
    },
    {
        city: '许昌',
        adcode: '411000'
    },
    {
        city: '周口',
        adcode: '411600'
    },
    {
        city: '沈阳',
        adcode: '210100'
    },
    {
        city: '大连',
        adcode: '210200'
    },
    {
        city: '哈尔滨',
        adcode: '230100'
    },
    {
        city: '长春',
        adcode: '220100'
    },
    {
        city: '拉萨',
        adcode: '540100'
    },
    {
        city: '乌鲁木齐',
        adcode: '650100'
    },  
    ]
}

// 转base64码
export const getBase64 = (file: any) => {
    return new Promise((resolve, reject) => {
        const reader = new FileReader()
        let fileResult: any = '';
        reader.readAsDataURL(file);
        reader.onload = () => {
        fileResult = reader.result
        };
        reader.onerror = (error) => {
        reject(error)
        };
        reader.onloadend = () => {
        resolve(fileResult)
        }
    })
}

// 压缩图片
import Compressor from 'compressorjs'

export const useCompressor = (file: any) => {
    return new Promise((resolve, reject) => {
        new Compressor(file, {
        //压缩质量, 0-1
        quality: 0.3,
        //转换的类型，默认为 image/png
        convertTypes: ['image/jpg'],
        //需要压缩的起始大小，默认5M, 5 * 1000 * 1000
        convertSize: 4000000,
        //压缩成功后处理
        success(result: any) {
            resolve(result)
        },
        error(err: any) {
            reject(err)
        }
        })
    })
}
// 用法：
// const uploadFileChange = (e: any, item: any, idx: number) => {
//   const _imgFile = e.target.files[0]
//   useCompressor(_imgFile).then((_imgFile: any) => {
//     const formData = new FormData()
//     formData.append('file', _imgFile)
//     formData.append('type', 'freight')
//     window.loading()
//     addImageStorage(formData).then((response) => {
//       window.loading(false)
//       if (response.status == '0') {
//         // deleteImg(3, item)
//         item.xingzhizhengbeimianImgObj = item.tracFileList[2] = {
//           fileUrl: response.data?.imageUrl,
//           fileType: '2_' + (idx + 1) + '_3',
//           fileId: '',
//           fileOperate: '1'
//         }
//       } else {
//         showToast(response.message)
//       }
//     })
//   })
// }

// 汇率货币对应数据
export const exchangeList = () => {
    return [
        { code: 'USD', name: '美元' },
        { code: 'EUR', name: '欧元' },
        { code: 'JPY', name: '日元' },
        { code: 'GBP', name: '英镑' },
        { code: 'CAD', name: '加元' },
        { code: 'AUD', name: '澳元' },
        { code: 'HKD', name: '港元' },
        { code: 'CHF', name: '瑞郎' },
        { code: 'SGD', name: '新元' },
        { code: 'NZD', name: '新西兰元' },
        { code: 'CNY', name: '人民币' },
    ]
}